package index;


import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;


public class Parser {

	public  ArrayList<org.apache.lucene.document.Document> allVerses = new ArrayList<org.apache.lucene.document.Document>();

	private boolean dbg = false;

	public   ArrayList<org.apache.lucene.document.Document>  parse (String fileName){

		int absoluteCounter = 0;

		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File(fileName)); 		//

			// normalize text representation
			doc.getDocumentElement ().normalize ();
			DBG("Root element of the doc is " + 
					doc.getDocumentElement().getNodeName());

			Element tstmt = doc.getDocumentElement();

			//coverpg...1/doc
			NodeList coverpgs = tstmt.getElementsByTagName("coverpg");
			Node currentCoverpg = coverpgs.item(0);
			NodeList coverpgTitle = ((Element)currentCoverpg).getElementsByTagName("title");
			String coverpgTitleString = coverpgTitle.item(0).getTextContent(); //remember


			//bookcoll or suracoll
			NodeList bookcoll = tstmt.getElementsByTagName("bookcoll");
			if (bookcoll.getLength() == 0) {
				bookcoll = tstmt.getElementsByTagName("suracoll");
			}

			Element firstBookcoll = (Element)bookcoll.item(0);

			//book or sura
			NodeList books = firstBookcoll.getElementsByTagName("book");
			if (books.getLength() == 0) {
				books = firstBookcoll.getElementsByTagName("sura");
			}

			for (int i = 0 ; i < books.getLength() ; i++) {
				Element currentBook = (Element)books.item(i);

				Element bktlong = (Element)currentBook.getElementsByTagName("bktlong").item(0);
				String firstBktLongString = bktlong.getTextContent();
				DBG("bktLong "+firstBktLongString);				//remembr							

				//bktShort
				Element bktshort = (Element)currentBook.getElementsByTagName("bktshort").item(0);
				String firstBktShortString = bktshort.getTextContent();				
				DBG("\tbktShort"+firstBktShortString);		//remembr

				NodeList epigraphs = currentBook.getElementsByTagName("epigraph");
				NodeList verses = currentBook.getElementsByTagName("v");

				String numOfVerses = new Integer(verses.getLength() + epigraphs.getLength()).toString();

				int j = 0;
				if (epigraphs.getLength() != 0) {
					org.apache.lucene.document.Document ep = new org.apache.lucene.document.Document();
					ep.add(new Field ("verseID", new Integer(j++).toString(), Store.YES, Field.Index.NO));
					ep.add(new Field ("verseContents", epigraphs.item(0).getTextContent(), Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
					ep.add(new Field ("bookId", new Integer(i).toString(), Field.Store.YES, Field.Index.NO));
					ep.add(new Field ("coverpgTitle", coverpgTitleString, Field.Store.YES, Field.Index.TOKENIZED));
					ep.add(new Field ("bktShort", firstBktShortString, Field.Store.COMPRESS, Field.Index.TOKENIZED));
					ep.add(new Field ("bktLong", firstBktLongString, Field.Store.COMPRESS, Field.Index.TOKENIZED));
					ep.add(new Field("fileName", fileName, Field.Store.YES, Field.Index.NO));
					ep.add(new Field("maxInCollection", numOfVerses, Field.Store.YES, Field.Index.NO));
					ep.add(new Field("absoluteID", new Integer(absoluteCounter++).toString(), Field.Store.YES, Field.Index.NO));
					allVerses.add(ep);
				}

				for (; j < verses.getLength(); j++) {
					org.apache.lucene.document.Document v = new org.apache.lucene.document.Document();
					v.add(new Field ("verseID", new Integer(j).toString(), Store.YES, Field.Index.NO));
					v.add(new Field ("verseContents",verses.item(j).getTextContent() , Field.Store.COMPRESS, Field.Index.TOKENIZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
					v.add(new Field ("bookId", new Integer(i).toString(), Field.Store.YES, Field.Index.NO));
					v.add(new Field ("coverpgTitle", coverpgTitleString, Field.Store.YES, Field.Index.TOKENIZED));
					v.add(new Field ("bktShort", firstBktShortString, Field.Store.COMPRESS, Field.Index.TOKENIZED));
					v.add(new Field ("bktLong", firstBktLongString, Field.Store.COMPRESS, Field.Index.TOKENIZED));
					v.add(new Field("fileName", fileName, Field.Store.YES, Field.Index.NO));
					v.add(new Field("maxInCollection", numOfVerses, Field.Store.YES, Field.Index.NO));
					v.add(new Field("absoluteID", new Integer(absoluteCounter++).toString(), Field.Store.YES, Field.Index.NO));
					allVerses.add(v);
				}



			}}	catch (SAXParseException err) {
				System.out.println ("** Parsing error" + ", line " 
						+ err.getLineNumber () + ", uri " + err.getSystemId ());
				System.out.println(" " + err.getMessage ());

			}catch (SAXException e) {
				Exception x = e.getException ();
				((x == null) ? e : x).printStackTrace ();

			}catch (Throwable t) {
				t.printStackTrace ();
			}


			return allVerses;

	}


	public void DBG(String msg) {
		if(dbg) {
			System.out.println("DBG: "+msg);	
		}
	}

}